import axios from './axios'
var qs = require('qs');
let instance = axios();
instance.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
import Vue from '../main'

instance.interceptors.response.use(
    response => {
        if(response.data.code==-200){
            Vue.$router.push({ path: "login" });
        }
        return response
    },

    error => {
        return Promise.reject(error.response.status) 
    })

export default {
    get(url, params) {
        return new Promise((resolve, reject) => {
            instance.get(url, {
                params: params
            }).then(res => {
                resolve(res.data);
            }).catch(err => {
            })
        })
    },

    post(url, params) {
        return new Promise((resolve, reject) => {
            instance.post(url, qs.stringify(params))
                .then(res => {
                    resolve(res.data);
                })
                .catch(err => {
                })
        });
    },
    postForm(url, formData, callback) {
        return new Promise((resolve, reject) => {
            instance.post(url, formData, {
                onUploadProgress: progressEvent => {
                    let complete = (progressEvent.loaded / progressEvent.total * 100 | 0) + '%'
                    callback({ "progress": complete })
                }
            })
                .then(res => {
                    resolve(res);
                })
                .catch(err => {
                })
        });
    }
}